﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;

public partial class CategoryDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlMuseum.DataSource = DBAccess.GetMuseumByCity("");
            ddlMuseum.DataBind();

            if (Session["MuseumId"] != null)
            {
                if (Convert.ToInt32(Session["MuseumId"]) > 0)
                {
                    ddlMuseum.SelectedValue = Session["MuseumId"].ToString();
                    ddlMuseum.Enabled = false;
                }
            }

            if (Request.QueryString["CategoryID"] != null)
                BindCategoryData();
        }
    }

    /// <summary>
    /// Binds my category data.
    /// </summary>
    private void BindCategoryData()
    {
        DataTable dtCategory = new DataTable();
        int categoryID = 0;
        try
        {
            if (Request.QueryString["CategoryID"] != null)
                categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
            dtCategory = DBAccess.GetCategoryDetails(categoryID);

            if (dtCategory != null && dtCategory.Rows.Count > 0)
            {
                txtCatergoryName.Text = dtCategory.Rows[0]["CategoryName"].ToString();
                ddlMuseum.SelectedValue = dtCategory.Rows[0]["MuseumId"].ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int categoryId = 0;
        int result = 0;
        try
        {
            if (Request.QueryString["CategoryID"] != null)
                categoryId = Convert.ToInt32(Request.QueryString["CategoryID"]);

            result = DBAccess.InsertOrUpdateCategory(Convert.ToInt32(ddlMuseum.SelectedItem.Value), txtCatergoryName.Text.Trim(), categoryId);

            if (categoryId < 1)
                Alert.ShowMessage("Category created successfully.");
            else
                Alert.ShowMessage("Category updated successfully.");

            Response.Redirect("ManageCategory.aspx", false);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        ddlMuseum.SelectedIndex = 0;
        txtCatergoryName.Text = "";
    }
}
